<?php

defined('BASEPATH') or die('forbidden');

class Statistics_Controller Extends Controller {

    function _init() {
        check_level(50, segment(0), segment(1), segment(2, 'index'));
    }

    function index() {
        $this->view->display("area/statistics/index");
    }

    function flexigrid() {
        if (isset($_POST['searchitems'])) {
            foreach ($_POST['searchitems'] as $m => &$n) {
                $t = explode("$$", $n);
                if (in_array($t[0], array('startdate', 'enddate', 'daytime'))) {
                    $n = "{$t[0]}$\${$t[1]}$$" . strtotime($t[2]);
                }
            }
        }
        list($colkey, $colsinfo, $where, $sortname, $sortorder, $offset, $rp, $page) = $this->common->get_flexigrid_params();
        $rows = array();
        $where = str_replace(array('startdate', 'enddate'), 'daytime', $where);
        $sql = "SELECT COUNT(*) AS num FROM {pre}area_statistics sa LEFT JOIN {pre}areasample a ON sa.areaId=a.id WHERE {$where}";
        $one = $this->db->one($sql);
        $total = $one['num'];
        $this->db->query("SELECT {$colsinfo} FROM {pre}area_statistics sa LEFT JOIN {pre}areasample a ON sa.areaId=a.id WHERE {$where}
                ORDER BY {$sortname} {$sortorder} 
                LIMIT {$offset},{$rp}");
        $ts = array();
        while ($n = $this->db->get_array('default', MYSQL_NUM)) {
            $n[1] = date("Y-m-d", $n[1]);
            $n[7] = $n[7] * 100 . "%";
            if (count($this->config['currency_list'] > 1)) {
                $revenue = 0;
                $revenuedata = json_decode($n[11]);
                if (is_array($revenuedata)) {
                    foreach ($revenuedata as $u => $v) {
                        //概率汇率进行转换为目标货币的总价
                        $revenue += (isset($this->config['currency_list'][$u]) ? (floatval($this->config['currency_list'][$u]['rate']) * $v) : $v);
                    }
                }
                $n[8] = $n[5] ? ceil($revenue / $n[5]) : 0;
                $n[9] = $n[6] ? ceil($revenue / $n[6]) : 0;
                $n[10] = sprintf("%.2f", $revenue);
            }
            $rows[] = array('id' => $n[0], 'cell' => array_values($n));
        }
        if (count($this->config['currency_list'] > 1)) {
            $ts = array(1 => 'Total', 4 => 0, 5 => 0, 6 => 0, 10 => 0);
            $data = array();
            $this->db->query("SELECT {$colsinfo} FROM {pre}area_statistics sa LEFT JOIN {pre}areasample a ON sa.areaId=a.id WHERE {$where}", "tot");
            while ($n = $this->db->get_array('tot', MYSQL_NUM)) {
                $ts[4]+=$n[4];
                $ts[5]+=$n[5];
                $ts[6]+=$n[6];
                $revenue = 0;
                $revenuedata = json_decode($n[11]);
                if (is_array($revenuedata)) {
                    foreach ($revenuedata as $u => $v) {
                        $data[$u] = isset($data[$u]) ? $data[$u] + $v : $v;
                        //概率汇率进行转换为目标货币的总价
                        $revenue += (isset($this->config['currency_list'][$u]) ? (floatval($this->config['currency_list'][$u]['rate']) * $v) : $v);
                    }
                }
                $ts[10] += sprintf("%.2f", $revenue);
            }
            $ts[11] = json_encode($data);
        } else {
            $f = array();
            foreach (explode(',', $colsinfo) as $k => $v)
                $f[] = "SUM({$v})";
            $ts = $this->db->one("SELECT " . implode(',', $f) . " FROM {pre}area_statistics sa LEFT JOIN {pre}areasample a ON sa.areaId=a.id WHERE {$where}", MYSQL_NUM);
            $ts[1] = 'Total';
            $ts[11] = json_encode(array($this->config['currency_revenue'] => $ts[10]));
            unset($ts[2], $ts[3], $ts[7], $ts[8], $ts[9], $ts[10]);
        }
        array_unshift($rows, array('id' => 0, 'cell' => $ts));
        $result = array(
            'page' => $page,
            'total' => $total,
            'rows' => $rows
        );
        echo json_encode($result);
    }

}